package com.minisoft.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.IsoFields;
import java.time.temporal.TemporalUnit;
import java.util.*;
import java.util.stream.Stream;

/**
 * @author JueSu
 * @description: TODO
 * @date 2022/10/29 7:39
 */
public class DateTest {

    @Test
    public void testJdTime() {
        while (true){
            String s = HttpUtil.get("https://api.m.jd.com/client.action?functionId=queryMaterialProducts&client=wh5");
            JSONObject jsonObject = JSONUtil.parseObj(s);
            System.out.println("Jd:" + jsonObject.getStr("currentTime"));
            System.out.println("LL:" + DateUtil.now());
            ThreadUtil.sleep(1000);
        }

    }
    @Test
    public void testBe() {
//        LocalDate date = LocalDate.now(); // 获取当前日期
        LocalDate date =LocalDate.of(2023, 1, 1); // 创建指定日期
        int year = date.getYear(); // 获取年份
        int month = date.getMonthValue(); // 获取月份
        int day = date.getDayOfMonth(); // 获取日期
        int week = date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR); // 获取周数
        int quarter = (month - 1) / 3 + 1; // 计算季度
        System.out.println("年份：" + year);
        System.out.println("月份：" + month);
        System.out.println("日期：" + day);
        System.out.println("周数：" + week);
        System.out.println("季度：" + quarter);

        System.out.println("年：" + date.getYear());
        System.out.println("月：" + String.format("%d-%02d", date.getYear(),date.getMonthValue()));
        System.out.println("日：" + date);
        System.out.println("周：" + String.format("%d-%02d", date.get(IsoFields.WEEK_BASED_YEAR),date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)));
        System.out.println("季：" + String.format("%d-%02d",date.getYear(),(date.getMonthValue() - 1) / 3 + 1));

        System.out.println(LocalDateTime.now().toString());
        LocalDate nowLocalDate = LocalDate.now();
        DateTime begin = DateUtil.parseDateTime("2022-12-10 21:17:01");
        DateTime end = DateUtil.parseDateTime("2022-12-10 01:17:01");
        Instant ei = Instant.ofEpochMilli(end.getTime());
        end.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        long between = DateUtil.between(DateUtil.beginOfDay(begin), DateUtil.beginOfDay(end), DateUnit.DAY,false);
        System.out.println(between);
    }

    @Test
    public void test1() {

        Date date1 = new Date(1688712884L);
        Date date2 = new Date(1688712884000L);
        long parse = DateUtil.parse("1970-01-20 12:41:22").getTime();
        Date date3 = new Date(parse * 1000);
        LocalDateTime firstDateTime = LocalDateTime.of(2021, 10, 1, 10, 30, 20);
        LocalDateTime secondDateTime = LocalDateTime.of(2021, 10, 1, 10, 30, 26);
        Duration duration = Duration.between(firstDateTime, secondDateTime);
        boolean isWithin5Seconds = duration.getSeconds() <= 5;


        //北京时间：Wed Jan 27 14:05:29 CST 2021
        Date date = new Date();

        String f = DateUtil.format(date, "MM月dd日");

        Date now = new Date();
        DateTime endDate = DateUtil.offsetDay(now, 90);
        DateTime dateTime1 = DateUtil.endOfDay(endDate);

        DateTime dateTime = DateUtil.parseDateTime("2022-11-03 21:17:01.638000000");

        SimpleDateFormat bjSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//北京时区
        bjSdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        System.out.println("毫秒数:" + date.getTime() + ", 北京时间:" + bjSdf.format(date));

//东京时区
        SimpleDateFormat tokyoSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tokyoSdf.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));  // 设置东京时区
        System.out.println("毫秒数:" + date.getTime() + ", 东京时间:" + tokyoSdf.format(date));

//如果直接print会自动转成当前时区的时间
        System.out.println(date);
//Wed Jan 27 14:05:29 CST 2021

    }
    @Test
    public void test2() {
        //当前时区时间
        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println("当前时区时间: " + zonedDateTime);

//东京时间
        ZoneId zoneId = ZoneId.of(ZoneId.SHORT_IDS.get("JST"));
        ZonedDateTime tokyoTime = zonedDateTime.withZoneSameInstant(zoneId);
        System.out.println("东京时间: " + tokyoTime);

// ZonedDateTime 转 LocalDateTime
        LocalDateTime localDateTime = tokyoTime.toLocalDateTime();
        System.out.println("东京时间转当地时间: " + localDateTime);

//LocalDateTime 转 ZonedDateTime
        ZonedDateTime localZoned = localDateTime.atZone(ZoneId.systemDefault());
        System.out.println("本地时区时间: " + localZoned);

//打印结果
//        当前时区时间: 2021-01-27T14:43:58.735+08:00[Asia/Shanghai]
//        东京时间: 2021-01-27T15:43:58.735+09:00[Asia/Tokyo]
//        东京时间转当地时间: 2021-01-27T15:43:58.735
//        当地时区时间: 2021-01-27T15:53:35.618+08:00[Asia/Shanghai]

    }

    @Test
    public void test3() {
        List<String> stringList = new ArrayList<>();
        stringList.add("ddd2");
        stringList.add("aaa2");
        stringList.add("bbb1");
        stringList.add("aaa1");
        stringList.add("bbb3");
        stringList.add("ccc");
        stringList.add("bbb2");
        stringList.add("ddd1");

        Optional<String> reduced =
                stringList
                        .stream()
                        .sorted()
                        .reduce((s1, s2) -> s1 + "#" + s2);

        reduced.ifPresent(System.out::println);//aaa1#aaa2#bbb1#bbb2#bbb3#ccc#ddd1#ddd2
        // 字符串连接，concat = "ABCD"
        String concat = Stream.of("A", "B", "C", "D").reduce("", String::concat);
// 求最小值，minValue = -3.0
        double minValue = Stream.of(-1.5, 1.0, -3.0, -2.0).reduce(Double.MAX_VALUE, Double::min);
// 求和，sumValue = 10, 有起始值
        int sumValue = Stream.of(1, 2, 3, 4).reduce(0, Integer::sum);
// 求和，sumValue = 10, 无起始值
        sumValue = Stream.of(1, 2, 3, 4).reduce(Integer::sum).get();
// 过滤，字符串连接，concat = "ace"
        concat = Stream.of("a", "B", "c", "D", "e", "F").
                filter(x -> x.compareTo("Z") > 0).
                reduce("", String::concat);

    }
}
